package com.zq.widget.inverted.game.role.damage;

import android.util.Log;
import android.view.animation.AnimationUtils;

import com.zq.widget.inverted.game.role.Role;
import com.zq.widget.inverted.game.role.RoleParent;
import com.zq.widget.inverted.game.role.RoleRoot;
import com.zq.widget.inverted.game.role.hp.HP;
import com.zq.widget.inverted.game.role.impl.plane.PlaneBoss;
import com.zq.widget.inverted.game.role.impl.type.Enemy;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangqiang on 2018/4/3.
 */
public class DamageCollisionHandler implements Role.CollisionHandler {

    private Map<String,Long> damageCache = new HashMap<>();
    private long damageInterval = 1000;
    private long point;

    @Override
    public void handleCollision(Role src, Role target) {

        if(hasRelationships(src,target)){
            return ;
        }

        Damage damageImpl = null;
        HP hpImpl = null;

        if (src instanceof Damage){
            damageImpl = (Damage) src;
        }
        if(target instanceof HP){
            hpImpl = (HP) target;
        }
//        if (target instanceof Damage && src instanceof HP) {
//            damage = (Damage) target;
//            hp = (HP) src;
//        }
        if(damageImpl != null && hpImpl != null){

            String key = getDamageKey(damageImpl,hpImpl);
            Long lastTime = damageCache.get(key);
            long timeMillis = AnimationUtils.currentAnimationTimeMillis();
            if(lastTime == null || timeMillis - lastTime > damageInterval){

                long damage = damageImpl.damage();
                hpImpl.reduceHp(damage);
                damageCache.put(key,timeMillis);

                if(hpImpl.getHp() <= 0){

                    addPoint(hpImpl.totalHp());

                    Role rootParent = getRootParent((Role) damageImpl);
                    if(rootParent != null && damageImpl != rootParent){

                        Role.CollisionHandler collisionHandler = rootParent.getCollisionHandler();
                        if(collisionHandler instanceof DamageCollisionHandler){
                            ((DamageCollisionHandler) collisionHandler).addPoint(hpImpl.totalHp());
                        }
                    }

                    if(hpImpl instanceof PlaneBoss){

                        Log.i("Test","===============" + hpImpl.totalHp());
                    }
                        Log.i("Test",damageImpl.getClass().getSimpleName() + "======击杀" + hpImpl.getClass().getSimpleName() + "====获取分数：" + hpImpl.totalHp() + "====总分数" + point);
                }
            }
        }
    }

    public long getPoint(){
        return point;
    }

    public void addPoint(long point){
        this.point += point;
    }

    private static String getDamageKey(Damage damage, HP hp) {
        return damage.toString() + "-" + hp.toString();
    }

    public static boolean hasRelationships(Role role,Role role2){

        Role rootParent = getRootParent(role);
        Role rootParent1 = getRootParent(role2);
        return rootParent == rootParent1 || rootParent instanceof Enemy && rootParent1 instanceof Enemy;
    }

    public static boolean isRootRole(Object role) {

        return role != null && RoleRoot.class.equals(role.getClass());
    }

    public static Role getRootParent(Role role){

        RoleParent parent = role.getParent();
        if(isRootRole(parent)){
            return role;
        }

        Role temp = role;
        while((parent = temp.getParent()) != null){

            if(isRootRole(parent)){
                return temp;
            }
            temp = (Role) parent;
        }
        return null;
    }
}
